﻿using ArtMath.Component;
using ArtMath.Resource;
using ArtMath.Core.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using ArtMath.Component.Point;

namespace ArtMath.Window
{
    public partial class CorrectCoordinateWindow : Form
    {
        [Browsable(false)]
        public PointBase SelectedPoint
        {
            get
            {
                if (listView1.SelectedItems.Count == 0)
                {
                    return null;
                }
                return listView1.SelectedItems[0].Tag as PointBase;
            }
        }
        [Browsable(false)]
        public LineBase SelectedLine
        {
            get
            {
                if (listView1.SelectedItems.Count == 0)
                {
                    return null;
                }
                return listView1.SelectedItems[0].Tag as LineBase;
            }
        }
        public CorrectCoordinateWindow(string pointName, PointD originCoordinate, ListViewItem[] items)
        {
            InitializeComponent();
            Text = $"{pointName} - {Resources.JoinLocalizedString("Coordinate", "Correction")}";
            cbDontCorrect.Text = $"{Resources.JoinLocalizedString("No2", "Correction")} {originCoordinate}";
            listView1.Items.AddRange(items);
            if (listView1.Items.Count > 0)
            {
                listView1.Items[0].Selected = true;
            }
        }
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnOK.Enabled = listView1.SelectedItems.Count > 0 || cbDontCorrect.Checked;
            cbByRef.Enabled = listView1.SelectedItems.Count > 0 && listView1.SelectedItems[0].Tag is PointBase;
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void cbDontCorrect_CheckedChanged(object sender, EventArgs e)
        {
            listView1.Enabled = !cbDontCorrect.Checked;
            if (!cbDontCorrect.Checked)
            {
                listView1_SelectedIndexChanged(sender, e);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            DialogResult = cbDontCorrect.Checked ? DialogResult.Cancel : DialogResult.OK;
        }

        private void listView1_ItemActivate(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }
    }
}
